package com.gm.baseplatform.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;

public class DateUtil{
	
	private DateUtil() {}

	public static Date getFirstDayOfYear(String format) {
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_YEAR, 1);
		return ca.getTime();
	}
	
	public static Date getFirstDayOfMonth() {
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_MONTH, 1);
		return ca.getTime();
	}

	public static Date getLastMonth(String format) {
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.DAY_OF_MONTH, 1);
		ca.set(Calendar.MONTH, ca.get(Calendar.MONTH) - 1);
		return ca.getTime();
	}

	public static Date getLastDayOfYear() {
		Calendar ca = Calendar.getInstance();
		ca.set(Calendar.MONTH, Calendar.DECEMBER);
		ca.set(Calendar.DAY_OF_MONTH, 30);
		return ca.getTime();
	}

	public static Date subDays(Date source, int i) {

		return new Date(source.getTime() - 86400000 * i);
	}

	public static Date subHours(Date source, int i) {
		return new Date(source.getTime() - 3600000 * i);
	}

	public static Date subMinius(Date source, int i) {
		return new Date(source.getTime() - 60000 * i);
	}

	public static Date subSeconds(Date source, int i) {
		return new Date(source.getTime() - 1000 * i);
	}
	
	public static Date addDays(Date source, int i) {

		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, i);
		return c.getTime();
	}

	public static Date addHours(Date source, int i) {
		return new Date(source.getTime() + 3600000 * i);
	}

	public static Date addMinius(Date source, int i) {
		return new Date(source.getTime() + 60000 * i);
	}

	public static Date addSeconds(Date source, int i) {
		return new Date(source.getTime() + 1000 * i);
	}
	
	public static void main(String[] args) {
		Date d = new Date();
		System.out.println(DateUtil.format(DateUtil.parse(DateUtil.getFirstDayOfMonth(), DateUtil.yyyyMMdd),DateUtil.yyyyMMddHHmmss));
		System.out.println(DateUtil.format(DateUtil.addSeconds(d, 1),
				DateUtil.yyyyMMddHHmmssSpt));
	}

	public static String format(String format) {
		return new SimpleDateFormat(format).format(Calendar.getInstance()
				.getTime());
	}

	public static String format(Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}

	public static Date parse(String date, String format) {
		try {
			return new SimpleDateFormat(format).parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static Date parse(Date date,String format) {
		try {
			return new SimpleDateFormat(format).parse(format(date,format));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	public static boolean checkDate(String inputDate,String datePattern){
		boolean isChecked = false;
		if(Pattern.matches(inputDate, datePattern)){
			isChecked = true;
		}
		return isChecked;
	}
	public static String yyyyMMddHHmmss = "yyyyMMddHHmmss";
	public static String yyyyMMddHHmm = "yyyyMMddHHmm";
	public static String yyyyMMdd = "yyyyMMdd";
	public static String yyyyMM = "yyyyMM";
	public static String HHmmss = "HHmmss";
	public static String HHmm = "HHmm";

	public static String yyyyMMddHHmmssSpt = "yyyy-MM-dd HH:mm:ss";
	public static String yyyyMMddHHmmSpt = "yyyy-MM-dd HH:mm";
	public static String yyyyMMddSpt = "yyyy-MM-dd";
	public static String yyyyMMSpt = "yyyy-MM";
	public static String HHmmssSpt = "HH:mm:ss";
	public static String HHmmSpt = "HH:mm";
}
